package com.nikkerball.web.converter;

import com.nikkerball.calculator.Calculation;
import com.nikkerball.calculator.Operator;
import com.nikkerball.web.base.BasePanel;
import com.nikkerball.web.renderers.ChoiceRenderer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

import java.math.BigDecimal;
import java.util.Arrays;

/**
 * TODO: Document class
 *
 * @author marius (created on 2010-02-18)
 */

public class SimpleCalculationPanel extends BasePanel {

    // ----- instance fields -----

    @SuppressWarnings({"UnusedDeclaration"})
    private Operator operator;

    @SuppressWarnings({"UnusedDeclaration"})
    private BigDecimal value1;

    @SuppressWarnings({"UnusedDeclaration"})
    private BigDecimal value2;

    private ComputationsPanel computationsPanel;

    // ----- constructors -----

    public SimpleCalculationPanel(String id, ComputationsPanel computationsPanel) {

        super(id);
        this.computationsPanel = computationsPanel;


        Form calculationForm = new Form("calculationForm");

        TextField<Double> value1TextField = new TextField<Double>("value1TextField",
                new PropertyModel<Double>(this, "value1"));

        DropDownChoice<Operator> operatorDropDownChoice = new DropDownChoice<Operator>("operatorDropDownChoice",
                new PropertyModel<Operator>(this, "operator"), Arrays.asList(Operator.values()),
                new ChoiceRenderer(this));

        TextField<Double> value2TextField = new TextField<Double>("value2TextField",
                new PropertyModel<Double>(this, "value2"));

        value1TextField.setRequired(true);
        operatorDropDownChoice.setRequired(true);
        value2TextField.setRequired(true);

        AjaxButton calculateButton = new AjaxButton("calculateButton") {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                Calculation calculation = new Calculation(value1, value2, operator);
                SimpleCalculationPanel.this.computationsPanel.getComputations().add(calculation);
                target.addComponent(SimpleCalculationPanel.this.computationsPanel);
            }
        };

        calculationForm.add(value1TextField);
        calculationForm.add(operatorDropDownChoice);
        calculationForm.add(value2TextField);
        calculationForm.add(calculateButton);

        add(calculationForm);

    }

}
